<template>
  <div>
    <a-time-picker
      v-model="timeStart"
      :defaultOpenValue="moment('00:00:00')"
      valueFormat="HH:mm:ss"
      placeholder="开始时间"
      :disabledHours="disabledStartHours"
      :disabledMinutes="disabledStartMinutes"
      :disabledSeconds="disabledStartSeconds"
      @openChange="handleStartTime"
      @change="onStartChange"
    />
    -
    <a-time-picker
      v-model="timeEnd"
      :defaultOpenValue="moment('23:59:59')"
      valueFormat="HH:mm:ss"
      placeholder="结束时间"
      :open="endOpen"
      :disabledHours="disabledEndHours"
      :disabledMinutes="disabledEndMinutes"
      :disabledSeconds="disabledEndSeconds"
      @openChange="handleEndTime"
    />
  </div>
</template>
<script>
import moment from 'moment'
import 'moment/locale/zh-cn'
export default {
  props: {
    setTimeStart: {
      type: String,
      required: false
    },
    setTimeEnd: {
      type: String,
      required: false
    }
  },
  data() {
    return {
      endOpen: false
    }
  },
  computed: {
    timeStart: {
      get() {
        return this.setTimeStart
      },
      set(val) {
        this.$emit('update:setTimeStart', val)
      }
    },
    timeEnd: {
      get() {
        return this.setTimeEnd
      },
      set(val) {
        this.$emit('update:setTimeEnd', val)
      }
    }
  },
  methods: {
    moment,
    setTime(start, end) {
      if (start) {
        this.timeStart = start
      }
      if (end) {
        this.timeEnd = end
      }
    },
    handleStartTime(open) {
      if (!open && !this.timeEnd) {
        this.endOpen = true
      }
    },
    handleEndTime(open) {
      this.endOpen = open
    },
    onStartChange(time) {
      // if (time && !this.timeEnd) this.timeEnd = time
    },
    disabledStartHours() {
      if (this.timeEnd) {
        let hours = []
        let timeArr = this.timeEnd.split(':')
        if (timeArr[1] == '00' && timeArr[2] == '00') {
          for (var i = parseInt(timeArr[0]); i < 24; i++) {
            hours.push(i)
          }
        } else {
          for (var i = parseInt(timeArr[0]) + 1; i < 24; i++) {
            hours.push(i)
          }
        }
        return hours
      }
    },
    disabledStartMinutes(selectedHour) {
      if (this.timeEnd) {
        let time = this.timeEnd
        let timeArr = time.split(':')
        let minutes = []
        if (selectedHour == parseInt(timeArr[0])) {
          for (var i = parseInt(timeArr[1]) + 1; i < 60; i++) {
            minutes.push(i)
          }
        }
        return minutes
      }
    },
    disabledStartSeconds(selectedHour, selectedMinute) {
      if (this.timeEnd) {
        let time = this.timeEnd
        let timeArr = time.split(':')
        let second = []
        if (selectedHour == parseInt(timeArr[0]) && selectedMinute == parseInt(timeArr[1])) {
          for (var i = parseInt(timeArr[2]); i <= 59; i++) {
            second.push(i)
          }
        }
        return second
      }
    },
    disabledEndHours() {
      if (this.timeStart) {
        let hours = []
        let timeArr = this.timeStart.split(':')
        for (var i = 0; i < parseInt(timeArr[0]); i++) {
          hours.push(i)
        }
        return hours
      }
    },
    disabledEndMinutes(selectedHour) {
      if (this.timeStart) {
        let time = this.timeStart
        let timeArr = time.split(':')
        let minutes = []
        if (selectedHour == parseInt(timeArr[0])) {
          for (var i = 0; i < parseInt(timeArr[1]); i++) {
            minutes.push(i)
          }
        }
        return minutes
      }
    },
    disabledEndSeconds(selectedHour, selectedMinute) {
      if (this.timeStart) {
        let time = this.timeStart
        let timeArr = time.split(':')
        let second = []
        if (selectedHour == parseInt(timeArr[0]) && selectedMinute == parseInt(timeArr[1])) {
          for (var i = 0; i <= parseInt(timeArr[2]); i++) {
            second.push(i)
          }
        }
        return second
      }
    }
  }
}
</script>

